package st.business.tepth.business.user.busnumber.workholder.repair.work.content;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.bean.PartBean;
import st.business.tepth.business.bean.ProgramBean;
import st.business.tepth.business.bean.RepairTaskBean;
import st.business.tepth.business.bean.post.ProgramConfirm;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/2/15
 */
public class RepairDataConvert {
    public List<MultipleItemEntity> programToMultipleItemEntity(RepairTaskBean taskBean) {
        //自己添加的方案已确认
        ArrayList<RepairTaskBean.FaultPhenomenonListBean> faultPhenomenonListBeans =
                taskBean.getFaultPhenomenonList();
        int status = taskBean.getMy_status();
        return programToMultipleItemEntity(faultPhenomenonListBeans, status);
    }

    public List<MultipleItemEntity> programToMultipleItemEntity(
            ArrayList<RepairTaskBean.FaultPhenomenonListBean> faultPhenomenonListBeans, int status
    ) {
        List<MultipleItemEntity> list = new ArrayList<>();
        //自己添加的方案已确认
        if (faultPhenomenonListBeans != null) {
            for (RepairTaskBean.FaultPhenomenonListBean faultPhenomenonListBean :
                    faultPhenomenonListBeans) {
                List<DiagnoseBean> tempList =
                        faultPhenomenonListBean.getFaultDiagnosticTemporaryList();
                if (tempList != null) {
                    List<MultipleItemEntity> itemEntities =
                            programToMultipleItemEntity(tempList, status);
                    if (!itemEntities.isEmpty()) {
                        list.addAll(itemEntities);
                    }
                }
            }
        }
        return list;
    }

    public List<MultipleItemEntity> programToMultipleItemEntity(List<DiagnoseBean> tempList, int status) {
        List<MultipleItemEntity> list = new ArrayList<>();
        for (DiagnoseBean diagnoseBean : tempList) {
            List<ProgramBean> programBeanList = diagnoseBean.getPlanList();
            if (programBeanList != null) {
                for (ProgramBean programBean : programBeanList) {
                    if (status < 3) {
                        if (programBean.getIs_confirmed() == 0) {
                            //系统推荐未确认
                            //programBean.setFault_phenomenon_id(diagnoseBean.getRepair_phenomenon_id());
                            list.add(MultipleItemEntity.builder()
                                    .setItemType(TroubleProgramAdapter.TYPE_SYSTEM_RECOMMEND_UN_CONFIRM)
                                    .setField(ItemFields.ITEM_OBJ, programBean)
                                    .build());
                        } else {
                            //已确认的就不管是否是推荐还是自己添加的
                            list.add(MultipleItemEntity.builder()
                                    .setItemType(TroubleProgramAdapter.TYPE_MANUAL_CONFIRM)
                                    .setField(ItemFields.ITEM_OBJ, programBean)
                                    .build());
                        }

                    } else {
                        //已完成
                        list.add(MultipleItemEntity.builder()
                                .setItemType(TroubleProgramAdapter.TYPE_CAN_NOT_EDIT)
                                .setField(ItemFields.ITEM_OBJ, programBean)
                                .build());
                    }
                }
            }
        }
        return list;
    }

    public List<MultipleItemEntity> programToMultipleItemEntityInMaintainDetail(List<DiagnoseBean> tempList, int status) {
        List<MultipleItemEntity> list = new ArrayList<>();
        for (DiagnoseBean diagnoseBean : tempList) {
            List<ProgramBean> programBeanList = diagnoseBean.getPlanList();
            if (programBeanList != null) {
                for (ProgramBean programBean : programBeanList) {
                    if (status < 3) {
                        //已确认的就不管是否是推荐还是自己添加的
                        list.add(MultipleItemEntity.builder()
                                .setItemType(TroubleProgramAdapter.TYPE_MANUAL_CONFIRM)
                                .setField(ItemFields.ITEM_OBJ, programBean)
                                .build());
                    } else {

                        //已完成
                        list.add(MultipleItemEntity.builder()
                                .setItemType(TroubleProgramAdapter.TYPE_CAN_NOT_EDIT)
                                .setField(ItemFields.ITEM_OBJ, programBean)
                                .build());
                    }
                }
            }
        }
        return list;
    }

    public String programListToJson(List<MultipleItemEntity> list, int repairId,
                                    int vehicle_series_id, int type) {
        List<ProgramConfirm> programConfirms = new ArrayList<>();
        if (list != null) {
            for (MultipleItemEntity entity : list) {
                ProgramConfirm programConfirm = new ProgramConfirm();
                programConfirm.setRepair_id(repairId);
                ProgramConfirm.Plan plan = new ProgramConfirm.Plan();
                ProgramBean programBean = entity.getField(ItemFields.ITEM_OBJ);
                plan.setFault_phenomenon_id(programBean.getPhenomenon_id());
                plan.setFault_diagnostic_id(programBean.getFault_diagnostic_id());
                plan.setVehicle_series_id(vehicle_series_id);
                plan.setSystem_repair_plan_id(programBean.getSystem_repair_plan_id());
                plan.setRelation_state(programBean.getRelation_state());
                plan.setId(programBean.getId());
                plan.setPositionCode(programBean.getPositionCode());
                //小修
                plan.setType(type);
                plan.setMaintenance_action_id(programBean.getMaintenance_action_id());
                List<PartBean> planParts = new ArrayList<>();
                //零部件
                PartBean part = programBean.getPart();
                if (part != null) {
                    part.setType(1);
                    planParts.add(part);
                }
                //耗材
                PartBean material = programBean.getMaterial();
                if (material != null) {
                    material.setType(2);
                    planParts.add(material);
                }
                plan.setPlanParts(planParts);

                programConfirm.setSceneRepairPlan(plan);
                //只提交未确认方案
                if (entity.getItemType() != TroubleProgramAdapter.TYPE_MANUAL_CONFIRM) {
                    programConfirms.add(programConfirm);
                }
            }
        }
        return JSON.toJSONString(programConfirms);
    }


}
